package com.bwsoft.admin.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.News;
import com.bwsoft.admin.domain.Report;
import com.bwsoft.admin.domain.valueObject.ReportVO;
import com.bwsoft.admin.service.ReportService;
import com.bwsoft.model.Message;



@Service(ReportService.NAME)
public class ReportServiceImpl extends BaseServiceImpl_ybd implements ReportService {
	private static Logger LOG = Logger.getLogger(ReportService.class.getClass());

	@Override
	public List<Report> findData(Integer page, Integer rows,
			String s_reportNumber, String s_sampleName, String s_danwei,
			String s_shangchuan, String s_start, String s_end) {
		
		StringBuffer reportHql = new StringBuffer();
		reportHql.append("from Report as rep where 1=1");
		if(s_reportNumber != null && !"".equals(s_reportNumber.trim())){
				reportHql.append(" and rep.reportNumber like '%"+s_reportNumber.trim()+"%'");
		}
		if(s_sampleName != null && !"".equals(s_sampleName.trim())){
			reportHql.append(" and rep.sampleName like '%"+s_sampleName.trim()+"%'");
		}
		if(s_danwei != null && !"".equals(s_danwei.trim())){
			reportHql.append(" and rep.danwei.compName like '%"+s_danwei.trim()+"%'");
		}
		if(s_shangchuan != null && !"".equals(s_shangchuan.trim())){
			reportHql.append(" and rep.shangchuan.compName like '%"+s_shangchuan.trim()+"%'");
		}
		if(s_start != null && !"".equals(s_start.trim())){
			reportHql.append(" and rep.addTime >= '"+s_start.trim()+"'");
		}
		if(s_end != null && !"".equals(s_end.trim())){
			reportHql.append(" and rep.addTime <= '"+s_end.trim()+"23:59:59'");
		}
		reportHql.append(" order by rep.addTime desc");
		List<Report> reportData = reportDao.findPage(reportHql.toString(), rows, (page - 1) * rows);
		
		return reportData;
	}

	@Override
	public Long getTotalCount(String s_reportNumber, String s_sampleName, String s_danwei,
			String s_shangchuan, String s_start, String s_end) {
		
		StringBuffer reportHql = new StringBuffer();
		reportHql.append("select count(*) from Report as rep where 1=1");
		if(s_reportNumber != null && !"".equals(s_reportNumber.trim())){
				reportHql.append(" and rep.reportNumber like '%"+s_reportNumber.trim()+"%'");
		}
		if(s_sampleName != null && !"".equals(s_sampleName.trim())){
			reportHql.append(" and rep.sampleName like '%"+s_sampleName.trim()+"%'");
		}
		if(s_danwei != null && !"".equals(s_danwei.trim())){
			reportHql.append(" and rep.danwei.compName like '%"+s_danwei.trim()+"%'");
		}
		if(s_shangchuan != null && !"".equals(s_shangchuan.trim())){
			reportHql.append(" and rep.shangchuan.compName like '%"+s_shangchuan.trim()+"%'");
		}
		if(s_start != null && !"".equals(s_start.trim())){
			reportHql.append(" and rep.addTime >= '"+s_start.trim()+"'");
		}
		if(s_end != null && !"".equals(s_end.trim())){
			reportHql.append(" and rep.addTime <= '"+s_end.trim()+"23:59:59'");
		}
		Long count;
		count = (Long)reportDao.findByHql(reportHql.toString()).get(0);
		return count;
	}

	@Override
	public Message remove(Integer id) {
		Message msg = new Message();
		Report report =reportDao.findById(id);
			try{
				reportDao.delete(report);
				msg.setMsgFlag(true);
				msg.setMsgStr("删除成功！");
			}catch (Exception e) {
				msg.setMsgFlag(false);
				msg.setMsgStr("删除失败："+e);
			}
		return msg;
	}

	@Override
	public List<ReportVO> getVo(List<Report> reports) {
		List<ReportVO> reportListVo = new ArrayList<ReportVO>();
		ReportVO reportVo = new ReportVO();
		for (Report report : reports) {
			reportVo = new ReportVO();
			reportVo.setAddTime(report.getAddTime());
			if(report.getDanwei() != null && !"".equals(report.getDanwei())){
				reportVo.setDanweiCompName(report.getDanwei().getCompName());
				reportVo.setDanweiEmail(report.getDanwei().getEmail());
				reportVo.setDanweiFax(report.getDanwei().getFax());
				reportVo.setDanweiId(report.getDanwei().getAccountId());
				reportVo.setDanweiLinkMan(report.getDanwei().getLinkMan());
			}
			if(report.getFile() != null && !"".equals(report.getFile())){
				reportVo.setFileId(report.getFile().getId().toString());
				reportVo.setFileName(report.getFile().getName());
				reportVo.setFilePath(report.getFile().getPath());
				reportVo.setFileSize(report.getFile().getSize());
			}
			reportVo.setReportId(report.getReportId());
			reportVo.setReportName(report.getReportName());
			reportVo.setReportNumber(report.getReportNumber());
			reportVo.setSampleName(report.getSampleName());
			reportVo.setScrqph(report.getScrqph());
			if(report.getShangchuan() != null && !"".equals(report.getShangchuan())){
				reportVo.setShangchuanCompName(report.getShangchuan().getCompName());
				reportVo.setShangchuanEmail(report.getShangchuan().getEmail());
				reportVo.setShangchuanFax(report.getShangchuan().getFax());
				reportVo.setShangchuanId(report.getShangchuan().getAccountId());
				reportVo.setShangchuanLinkMan(report.getShangchuan().getLinkMan());
				reportVo.setShangchuanPhone(report.getShangchuan().getPhone());
			}
			reportListVo.add(reportVo);
		}
		return reportListVo;
	}

	
}
